import React, { useCallback } from 'react';
import { LogoutOutlined } from '@ant-design/icons';
import { Avatar, Menu, Spin } from 'antd';
import HeaderDropdown from '../HeaderDropdown';
import styles from './index.module.less';
import type { MenuInfo } from 'rc-menu/lib/interface';
import { useNavigate } from 'react-router-dom';
import { ItemType } from 'antd/lib/menu/hooks/useItems';
import logo from '/logo.svg';

export type GlobalHeaderRightProps = {
  menu?: boolean;
};

const AvatarDropdown: React.FC<GlobalHeaderRightProps> = () => {
  const navigator = useNavigate();

  const onMenuClick = useCallback((event: MenuInfo) => {
    const { key } = event;
    if (key === 'logout') {
      localStorage.removeItem('token');
      navigator('/login');
    }
    if (key === 'userInfo') {
      // navigator('/');
    }
  }, []);

  const loading = (
    <span className={`${styles.action} ${styles.account}`}>
      <Spin
        size="small"
        style={{
          marginLeft: 8,
          marginRight: 8,
        }}
      />
    </span>
  );

  const items: ItemType[] = [
    {
      key: 'userInfo',
      label: (
        <>
          <i className="iconfont icon-user-fill" style={{ color: 'rgb(78, 78, 78)' }} />
          &nbsp;&nbsp;账号信息
        </>
      ),
    },

    {
      key: 'logout',
      label: (
        <>
          <LogoutOutlined />
          退出登录
        </>
      ),
    },
  ];

  const menuHeaderDropdown = <Menu className={styles.menu} items={items} onClick={onMenuClick} />;

  return (
    <HeaderDropdown overlay={menuHeaderDropdown}>
      <span className={`${styles.action} ${styles.account}`}>
        <Avatar size="small" className={styles.avatar} src={logo} alt="avatar" />

        <span className={`${styles.name} anticon`}>{'name'}</span>
      </span>
    </HeaderDropdown>
  );
};

export default AvatarDropdown;
